﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCT.CodeBox.SimplestStringTemplate.Codes;

namespace DCT.CodeBox.SimplestStringTemplate
{
    class Program
    {
        static void Main(string[] args)
        {
            //Test();
            Test2();
        }

        public static void Test2() {
            string t = "#UserName#邀请了#Friend#";
            Console.WriteLine(SST.ToString(t
                , SST.DictionaryReplacement(new Dictionary<String, string>() { { "UserName", "小明" }, { "Friend", "小华" } }
                , "")));
        }

        public static void Test()
        {
            //#xxx#，为模版的Key，默认为#为分隔符
            string t1 = "#UserName#邀请了#Friend#";
            string result = SST.ToString(t1, delegate(char _d, string _k)
            {
                switch (_k)
                {
                    case "UserName":
                        {
                            return "小明";
                        }
                    case "Friend":
                        {
                            return "小华";
                        }
                }
                return "";
            });
            Console.WriteLine(result);  //小明邀请了小华

            //如果要显示#，则用连续2个#（即##）为转义符；##Friend##是想显示“#Friend#”，因此不会显示小华
            t1 = "###UserName###邀请了##Friend##";
            result = SST.ToString(t1, delegate(char _d, string _k)
            {
                switch (_k)
                {
                    case "UserName":
                        {
                            return "小明";
                        }
                    case "Friend":
                        {
                            return "小华";
                        }
                }
                return "";
            });
            Console.WriteLine(result);  //#小明#邀请了#Friend#

            //key可以有其他字符，如下面的Key为“User\#Name”，但不建议这么写
            t1 = @"###User\#Name###邀请了###Friend###";
            result = SST.ToString(t1, delegate(char _d, string _k)
            {
                switch (_k)
                {
                    case "UserName":
                        {
                            return "小明";
                        }
                    case "Friend":
                        {
                            return "小华";
                        }
                    case "User#Name":
                        {
                            return "小明xxx";
                        }
                }
                return "";
            });
            Console.WriteLine(result);  //#小明xxx#邀请了#小华#

            //key为其他特殊字符的例子，但不建议这么写
            t1 = @"###User|Name###邀请了|||Fri\#\|end|";
            result = SST.ToString(t1, new char[] { '|', '#' }, delegate(char _d, string _k)
            {
                Console.WriteLine("Key__" + _k);
                switch (_k)
                {
                    case "User|Name":
                        {
                            return "小明";
                        }
                    case "Fri\\#|end":
                        {
                            return "小华";
                        }
                }
                return "";
            });
            Console.WriteLine(result);  //#小明#邀请了|小华
        }
    }
}
